//---------------------------------------------------------------------
// File: ReceivePortConductorStep.cs
// 
// Summary: 
//
//---------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
// PURPOSE.
//---------------------------------------------------------------------

namespace Microsoft.Services.BizTalkApplicationFramework.BizUnit
{
	using System;
	using System.Management;
	using System.Xml;
	using Microsoft.Services.BizTalkApplicationFramework.BizUnit;

	/// <summary>
	/// The ReceivePortConductorStep enables/dissables a recieve location.
	/// </summary>

	/// <remarks>
	/// The following shows an example of the Xml representation of this test step.
	/// 
	/// <code escaped="true">
    /// <TestStep assemblyPath="" typeName="Microsoft.Services.BizTalkApplicationFramework.BizUnit.ReceivePortConductorStep, Microsoft.Services.BizTalkApplicationFramework.BizUnit.BizTalkSteps">
	///		<DelayForCompletion>5</DelayForCompletion> <!-- Optional, seconds to delay for this step to complete -->
	///		<ReceivePortName></ReceivePortName>
	///		<ReceiveLocationName></ReceiveLocationName>
	///		<Action>Enable</Action>
	/// </TestStep>
	/// </code>
	///	
	///	<list type="table">
	///		<listheader>
	///			<term>Tag</term>
	///			<description>Description</description>
	///		</listheader>
	///		<item>
	///			<term>DelayForCompletion</term>
	///			<description>The number of seconds to deplay for this step to complete</description>
	///		</item>
	///		<item>
	///			<term>ReceivePortName</term>
	///			<description>The name of the receive port to containing the receive location to enable/dissable</description>
	///		</item>
	///		<item>
	///			<term>ReceiveLocationName</term>
	///			<description>The name of the receive location to enable/dissable</description>
	///		</item>
	///		<item>
	///			<term>Action</term>
	///			<description>Enable|Disable</description>
	///		</item>
	///	</list>
	///	</remarks>

	public class ReceivePortConductorStep : ITestStep
	{
		internal enum ReceivePortAction 
		{
			Enable,
			Disable,
		}

		#region ITestStep Members

		/// <summary>
		/// ITestStep.Execute() implementation
		/// </summary>
		/// <param name='testConfig'>The Xml fragment containing the configuration for this test step</param>
		/// <param name='context'>The context for the test, this holds state that is passed beteen tests</param>
		public void Execute(XmlNode testConfig, Context context)
		{
			string receivePortName = context.ReadConfigAsString(testConfig, "ReceivePortName");
			string receiveLocationName = context.ReadConfigAsString(testConfig, "ReceiveLocationName");

			ReceivePortAction action = (ReceivePortAction)Enum.Parse( typeof(ReceivePortAction), context.ReadConfigAsString(testConfig, "Action"), true );

			if ( action == ReceivePortAction.Enable )
			{
				Enable( receivePortName, receiveLocationName, context );
			}
			else
			{
				Disable( receivePortName, receiveLocationName, context );
			}

			// Delay if necessary to allow the orchestration to start/stop
			int delayForCompletion = context.ReadConfigAsInt32(testConfig, "DelayForCompletion", true);
			if (delayForCompletion > 0)
			{
				context.LogInfo("Waiting for {0} seconds before recommencing testing.", delayForCompletion);
				System.Threading.Thread.Sleep(delayForCompletion*1000);
			}
		}

		#endregion

		private static void Enable( string receivePortName, string receiveLocationName, Context context )
		{
			string WMIQuery = String.Format(	"Select * from MSBTS_ReceiveLocation where ReceivePortName=\"{0}\" and Name=\"{1}\"",
				receivePortName, receiveLocationName);

			EnumerationOptions enumOptions = new EnumerationOptions();
			enumOptions.ReturnImmediately = false;

			ManagementObjectSearcher OrchestrationSearcher = new ManagementObjectSearcher("root\\MicrosoftBizTalkServer",	WMIQuery, enumOptions);							

			foreach( ManagementObject OrchestrationInstance in OrchestrationSearcher.Get())
			{
				context.LogInfo("Enabling Receive Port {0} at location {1}", receivePortName, receiveLocationName );
				OrchestrationInstance.InvokeMethod("Enable", new object[] {2,2});
			}																													
		}

		private static void Disable( string receivePortName, string receiveLocationName, Context context )
		{
			string WMIQuery = String.Format(	"Select * from MSBTS_ReceiveLocation where ReceivePortName=\"{0}\" and Name=\"{1}\"",
				receivePortName, receiveLocationName);

			EnumerationOptions enumOptions = new EnumerationOptions();
			enumOptions.ReturnImmediately = false;

			ManagementObjectSearcher OrchestrationSearcher = new ManagementObjectSearcher("root\\MicrosoftBizTalkServer",	WMIQuery, enumOptions);							

			foreach( ManagementObject OrchestrationInstance in OrchestrationSearcher.Get())
			{
				context.LogInfo("Disabling Receive Port {0} at location {1}", receivePortName, receiveLocationName );
				OrchestrationInstance.InvokeMethod("Disable", new object[] {2,2});
			}																													
		}
	}
}
